# 把当前项目根目录加入Python环境路径
import sys
sys.path.append('/www/wwwroot/panda-ai')

from models.user_keys import UserKey, UsedLog
from models.base import get_db


def reset_key(key_str: str, set_remainder: int = 100):
    db = next(get_db())
    try:
        # 查询所有key记录
        userkey = UserKey().read(db, key_str)
        if not userkey:
            print(f'未找到key: {key_str}')
            return False
        # 重置key
        userkey.remainder = set_remainder
        userkey.update(db, key_str, set_remainder)
        print(f'重置key: {key_str} 成功, 剩余次数: {set_remainder}')
        return True

    except Exception as e:
        print(f'查询失败! {e}')

if __name__ == "__main__":
    reset_key('755cc816-b6ec-4f02-9232-4eac1f2f3c40')